--
-- Author: yawei.zhu
-- Date: 2018-07-13 15:54:26
--

local PvpChampionFinalTeamInfo = class("PvpChampionFinalTeamInfo")

function PvpChampionFinalTeamInfo:ctor(info)
	if info then
		self:read(info)
	end
end

function PvpChampionFinalTeamInfo:read(info)
	self.isKnockOut = info.IsKnockOut

	local teamInfos = info.teams
	local teams = {}
	if teamInfos then
		for _, data in ipairs(teamInfos) do
			local value = {}
			value.part = data.Part
			value.idx = data.Idx
			value.teamName = string.format(L("throneTeamName"), data.TeamName)
			value.round = data.Round
			value.teamId = data.TeamID
			table.insert(teams, value)
		end
	end

	local results = info.infos
	local battleResults = {}
	if results then
		for _, data in ipairs(results) do
			local value = {}
			value.TeamA = data.TeamA
			value.TeamB = data.TeamB
			value.WonTeam = data.WonTeam
			value.battleID = data.battleID
			value.ScoreA = data.ScoreA
			value.ScoreB = data.ScoreB
			table.insert(battleResults, value)
		end
	end
	self._battleResults = battleResults

	local function sortTeams(a, b)
		if a.part == b.part then
			return a.idx < b.idx
		else
			return a.part < b.part
		end
	end

	table.sort(teams, sortTeams)
	for i, data in ipairs(teams) do
		data.index = i -- 1-16
	end
	self._teamInfos = teams

	print("isKnockOut : ", self.isKnockOut)
	-- dump(teams)
end

function PvpChampionFinalTeamInfo:test()
	local teamInfos = {}

	for i = 1, 64 do
		local value = {}
		value.Part = (i <= 32) and 0 or 1 -- 上半区0，下半区1
		value.Idx = (i <= 32) and i or (i - 32) -- 上半区1-32， 下半区1-32
		value.TeamName = "team"..i
		value.Round = 0
		value.TeamID = i
		table.insert(teamInfos, value)
	end

	teamInfos[1].Round = 3
	teamInfos[4].Round = 1
	teamInfos[5].Round = 1
	teamInfos[7].Round = 2

	teamInfos[10].Round = 6
	teamInfos[11].Round = 1
	teamInfos[14].Round = 1
	teamInfos[16].Round = 2

	local battleResults = {}
	local value = {}
	value.TeamA = 1
	value.TeamB = 2
	value.battleID = 10
	value.WonTeam = 1
	value.ScoreA = 8
	value.ScoreB = 5
	table.insert(battleResults, value)

	value = {}
	value.TeamA = 1
	value.TeamB = 10
	value.battleID = 10
	value.WonTeam = 10
	value.ScoreA = 4
	value.ScoreB = 7
	table.insert(battleResults, value)

	self:read({IsKnockOut = true, teams = teamInfos, infos = battleResults})
end

function PvpChampionFinalTeamInfo:getTeamInfos()
	return self._teamInfos
end

function PvpChampionFinalTeamInfo:getBattleResults()
	return self._battleResults
end

function PvpChampionFinalTeamInfo:getTeamInfoById(teamId)
	if not teamId or teamId <= 0 then return nil end
	for _, team in ipairs(self._teamInfos) do
		if team.teamId == teamId then
			return team
		end
	end
	return nil
end

function PvpChampionFinalTeamInfo:checkIsKnockOut(teamId)
	local value = self:getTeamInfoById(teamId)
	if not value then return true end
	local index = value.index

	local startIdx, endIdx = 0, -1
	if value.round == 0 then -- 判断所在区域的2个
		endIdx = math.ceil(index / 2) * 2
		startIdx = endIdx - 1
	elseif value.round == 1 then -- 判断所在区域的4个
		endIdx = math.ceil(index / 4) * 4
		startIdx = endIdx - 3
	elseif value.round == 2 then -- 判断所在区域的8个
		endIdx = math.ceil(index / 8) * 8
		startIdx = endIdx - 7
	elseif value.round == 3 then -- 判断所在区域的16个
		endIdx = math.ceil(index / 16) * 16
		startIdx = endIdx - 15
	elseif value.round == 4 then -- 判断所在区域的32个
		endIdx = math.ceil(index / 32) * 32
		startIdx = endIdx - 31
	elseif value.round == 5 then -- 判断所有的64个
		endIdx = 64
		startIdx = 1
	end

	for i = startIdx, endIdx do
		if self._teamInfos[i].round > value.round then 
			return true 
		end
	end

	return false
end

-- 获取参加决赛的两支队伍，如果不是决赛返回空（有且只有两个队伍的round为5）
function PvpChampionFinalTeamInfo:getFinalWarTeams()
	local count = 0
	local teams = {}
	for _, team in ipairs(self._teamInfos) do
		if team.round >= 6 then return nil end
		if team.round == 5 then 
			count = count + 1
			table.insert(teams, team)
		end
	end
	if count == 2 then return teams end
	return nil
end

-- 获取冠军队伍，如果还未产生冠军，返回nil
function PvpChampionFinalTeamInfo:getChampionTeam()
	for _, team in ipairs(self._teamInfos) do
		if team.round >= 6 then return team end
	end
	return nil
end

return PvpChampionFinalTeamInfo
